package com.fumasoft.dynamic.utils;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.CannedAccessControlList;
import com.fumasoft.dynamic.config.OssConfig;
import com.fumasoft.dynamic.contant.Constants;
import java.io.File;
import java.net.URL;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * OSS工具类.
 *
 * @Author: jingchen
 * @Date: 2025/08/30
 */
@Component
public class OssUtils {

    private static final Integer TIME_OUT = 50000;

    private static final Long MILLIS_PER_SECOND = 1000L;

    private static final Long SECONDS_PER_MINUTE = 60L;

    @Resource
    private OSS oss;

    @Resource
    private OssConfig ossConfig;

    /**
     * 上传文件.
     *
     * @param cId       企业
     * @param fileName  文件名
     * @param file      文件
     * @param expireMinute  过期时间 单位分钟
     * @return 文件下载链接
     */
    public String upload(Integer cId, String fileName, File file, Integer expireMinute) {
        String ossKey = Constants.oss_past_one_day + cId + "/"
                + System.currentTimeMillis() + "/" + fileName;

        oss.putObject(ossConfig.getBucketName(), ossKey, file);
        // 设置Object的访问权限为公共读。
        oss.setObjectAcl(ossConfig.getBucketName(), ossKey, CannedAccessControlList.PublicRead);

        return getOssUrl(ossConfig.getBucketName(), ossKey, expireMinute);
    }

    /**
     * 上传文件.
     *
     * @param cId       企业
     * @param fileName  文件名
     * @param file      文件
     * @return 文件下载链接
     */
    public String upload(Integer cId, String fileName, File file) {
        String ossKey = Constants.oss_past_one_day + cId + "/"
                + System.currentTimeMillis() + "/" + fileName;

        oss.putObject(ossConfig.getBucketName(), ossKey, file);
        // 设置Object的访问权限为公共读。
        oss.setObjectAcl(ossConfig.getBucketName(), ossKey, CannedAccessControlList.PublicRead);

        return getOssUrl(ossConfig.getBucketName(), ossKey).replace("-internal", "").replace("http://", "https://");
    }

    /**
     * 获取文件下载链接 .
     *
     * @param bucketName  bucketName
     * @param key key
     * @param expiredMinute expiredMinute
     * @return 文件下载链接
     */
    public String getOssUrl(String bucketName, String key, int expiredMinute) {
        if (StringUtils.isBlank(bucketName)) {
            bucketName = ossConfig.getBucketName();
        }
        ClientBuilderConfiguration configuration = new ClientBuilderConfiguration();
        configuration.setConnectionTimeout(TIME_OUT);
        Date expiration = new Date(System.currentTimeMillis() + expiredMinute * MILLIS_PER_SECOND * SECONDS_PER_MINUTE);
        URL url = oss.generatePresignedUrl(bucketName, key, expiration);
        String urlStr = url.toString();
        return urlStr.replace("-internal", "").replace("http://", "https://");
    }

    /**
     * 获取文件下载链接 .
     *
     * @param bucketName  bucketName
     * @param key key
     * @return 文件下载链接
     */
    public String getOssUrl(String bucketName, String key) {
        if (StringUtils.isBlank(bucketName)) {
            bucketName = ossConfig.getBucketName();
        }
        String endpointWithBucket = ossConfig.getEndpoint().replace("://", "://" + bucketName + ".");
        return endpointWithBucket + "/" + key;
    }
}
